package com.eat.servicebase.config;


import com.eat.servicebase.Interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CZF
 * @create 2022-03-20 18:55
 *
 * 用于管理HandlerInterceptor拦截器
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    TokenInterceptor tokenInterceptor;

    /**
     * 配置拦截器链
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> list = new ArrayList<>();

//        放行所有请求：生产环境注释掉
        list.add("/**");

        //websocket
        list.add("/webSocket/**");


        //swagger
        list.add("/swagger-resources/**");
        list.add("/webjars/**");
//        list.add("/v2/**");
        list.add("/swagger-ui.html/**");


        list.add("/wx/**");
//        list.add("/web/equipment/**");
        list.add("/error");
        list.add("/getPic/**"); //获取图片请求
        list.add("/favicon.ico/**");

        //web用户 注册、登录、验证码
        list.add("/web/user/**");

        //开放图片请求路径
        list.add("/getDetailFish/**");
        list.add("/getDetailDisease/**");
        list.add("/getAvater/**");
        list.add("/getPic/**");
        list.add("/getFishBaiKe/**");
        list.add("/getDiseaseBaiKe/**");
        list.add("/getqualificationOne/**");
        list.add("/getqualificationTwo/**");
        list.add("/getDiseasePic/**");
        list.add("/getWxGg/**");


//        list.add("/**");

        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(list);

    }

    //配置本地文件映射到url上
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 鱼类详细图片资源转发 鱼类百科路径
         */
        String uploadDetailFishPath = "/root/platform_fish/fish/img/fish_detailFish_img/";
        registry.addResourceHandler("/getDetailFish/**")
                .addResourceLocations("file:" + uploadDetailFishPath);

        /**
         * 鱼病详细图片资源转发 鱼病百科路径
         */
        String uploadDetailDiseasePath = "/root/platform_fish/fish/img/fish_detailDisease_img/";
        registry.addResourceHandler("/getDetailDisease/**")
                .addResourceLocations("file:" + uploadDetailDiseasePath);

        /**
         * 头像资源转发
         */
        String uploadPathAvater = "/root/platform_fish/fish/img/avater/";
        registry.addResourceHandler("/getAvater/**")
                .addResourceLocations("file:" + uploadPathAvater);

        /**
         * 鱼的照片资源转发
         */
//        String uploadPath="/D:/projectFile2/fish/%E5%90%8E%E7%AB%AF/img/fishImg/";    //本地地址
        String uploadPathFish = "/root/platform_fish/fish/img/fishImg/";     //linux服务器地址

        //修改tomcat 虚拟映射
        //相当于把原来请求地址：http:ip:port/getPic/**  转发为 file:///D:/projectFile2/fish/%E5%90%8E%E7%AB%AF/img/fishImg/
        registry.addResourceHandler("/getPic/**")
                .addResourceLocations("file:" + uploadPathFish);  //图片存放路径


        /**
         * 鱼类百科资源转发
         */
        String uploadPathFishBaiKe = "/root/platform_fish/fish/img/fish_baike_img/";
        registry.addResourceHandler("/getFishBaiKe/**")
                .addResourceLocations("file:" + uploadPathFishBaiKe);

        /**
         * 鱼病百科资源转发
         */
        String uploadPathDiseaseBaiKe = "/root/platform_fish/fish/img/fish_disease_img/";
        registry.addResourceHandler("/getDiseaseBaiKe/**")
                .addResourceLocations("file:" + uploadPathDiseaseBaiKe);

        /**
         * 专家资质一资源转发
         */
        String getqualificationOne = "/root/platform_fish/fish/img/expert_qualificationOne/";
        registry.addResourceHandler("/getqualificationOne/**")
                .addResourceLocations("file:" + getqualificationOne);


        /**
         * 专家资质二资源转发
         */
        String getqualificationTwo = "/root/platform_fish/fish/img/expert_qualificationTwo/";
        registry.addResourceHandler("/getqualificationTwo/**")
                .addResourceLocations("file:" + getqualificationTwo);


        /**
         * 问诊表资源转发
         */
        String uploadDiseasePath = "/root/platform_fish/fish/img/disease/";
        registry.addResourceHandler("/getDiseasePic/**")
                .addResourceLocations("file:" + uploadDiseasePath);


        /**
         * 微信小程序未登录页面背景图
         */
        String getWxBgPath = "/root/platform_fish/fish/img/wxBg/";
        registry.addResourceHandler("/getWxGg/**")
                .addResourceLocations("file:" + getWxBgPath);
    }
}


